<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="ctypes-calling-functions-continued.html" />
<link rel="prev" href="ctypes-calling-functions.html" />
<link rel="parent" href="ctypes-ctypes-tutorial.html" />
<link rel="next" href="ctypes-calling-functions-continued.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>14.14.1.4 Fundamental data types</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="14.14.1.3 calling functions"
  href="ctypes-calling-functions.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="14.14.1 ctypes tutorial"
  href="ctypes-ctypes-tutorial.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="14.14.1.5 calling functions, continued"
  href="ctypes-calling-functions-continued.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="ctypes-calling-functions.html">14.14.1.3 Calling functions</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="ctypes-ctypes-tutorial.html">14.14.1 ctypes tutorial</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="ctypes-calling-functions-continued.html">14.14.1.5 Calling functions, continued</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h3><a name="SECTION00161414000000000000000"></a><a name="ctypes-fundamental-data-types"></a>
<br>
14.14.1.4 Fundamental data types
</h3>

<p>
<code>ctypes</code> defines a number of primitive C compatible data types :
<blockquote>
</blockquote><div class="center"><table class="realtable">
  <thead>
    <tr>
      <th class="left"  >
ctypes type
</th>
      <th class="left"  >
C type
</th>
      <th class="left"  >
Python type
</th>
      </tr>
    </thead>
  <tbody>
    <tr><td class="left"   valign="baseline">
<tt class="class">c_char</tt>
</td>
        <td class="left"  >
<code>char</code>
</td>
        <td class="left"  >
1-character
string
</td></tr>
    <tr><td class="left"   valign="baseline">
<tt class="class">c_wchar</tt>
</td>
        <td class="left"  >
<code>wchar_t</code>
</td>
        <td class="left"  >
1-character
unicode string
</td></tr>
    <tr><td class="left"   valign="baseline">
<tt class="class">c_byte</tt>
</td>
        <td class="left"  >
<code>char</code>
</td>
        <td class="left"  >
int/long
</td></tr>
    <tr><td class="left"   valign="baseline">
<tt class="class">c_ubyte</tt>
</td>
        <td class="left"  >
<code>unsigned char</code>
</td>
        <td class="left"  >
int/long
</td></tr>
    <tr><td class="left"   valign="baseline">
<tt class="class">c_short</tt>
</td>
        <td class="left"  >
<code>short</code>
</td>
        <td class="left"  >
int/long
</td></tr>
    <tr><td class="left"   valign="baseline">
<tt class="class">c_ushort</tt>
</td>
        <td class="left"  >
<code>unsigned short</code>
</td>
        <td class="left"  >
int/long
</td></tr>
    <tr><td class="left"   valign="baseline">
<tt class="class">c_int</tt>
</td>
        <td class="left"  >
<code>int</code>
</td>
        <td class="left"  >
int/long
</td></tr>
    <tr><td class="left"   valign="baseline">
<tt class="class">c_uint</tt>
</td>
        <td class="left"  >
<code>unsigned int</code>
</td>
        <td class="left"  >
int/long
</td></tr>
    <tr><td class="left"   valign="baseline">
<tt class="class">c_long</tt>
</td>
        <td class="left"  >
<code>long</code>
</td>
        <td class="left"  >
int/long
</td></tr>
    <tr><td class="left"   valign="baseline">
<tt class="class">c_ulong</tt>
</td>
        <td class="left"  >
<code>unsigned long</code>
</td>
        <td class="left"  >
int/long
</td></tr>
    <tr><td class="left"   valign="baseline">
<tt class="class">c_longlong</tt>
</td>
        <td class="left"  >
<code>__int64</code> or
<code>long long</code>
</td>
        <td class="left"  >
int/long
</td></tr>
    <tr><td class="left"   valign="baseline">
<tt class="class">c_ulonglong</tt>
</td>
        <td class="left"  >
<code>unsigned __int64</code> or
<code>unsigned long long</code>
</td>
        <td class="left"  >
int/long
</td></tr>
    <tr><td class="left"   valign="baseline">
<tt class="class">c_float</tt>
</td>
        <td class="left"  >
<code>float</code>
</td>
        <td class="left"  >
float
</td></tr>
    <tr><td class="left"   valign="baseline">
<tt class="class">c_double</tt>
</td>
        <td class="left"  >
<code>double</code>
</td>
        <td class="left"  >
float
</td></tr>
    <tr><td class="left"   valign="baseline">
<tt class="class">c_char_p</tt>
</td>
        <td class="left"  >
<code>char *</code>
(NUL terminated)
</td>
        <td class="left"  >
string or
<code>None</code>
</td></tr>
    <tr><td class="left"   valign="baseline">
<tt class="class">c_wchar_p</tt>
</td>
        <td class="left"  >
<code>wchar_t *</code>
(NUL terminated)
</td>
        <td class="left"  >
unicode or
<code>None</code>
</td></tr>
    <tr><td class="left"   valign="baseline">
<tt class="class">c_void_p</tt>
</td>
        <td class="left"  >
<code>void *</code>
</td>
        <td class="left"  >
int/long
or <code>None</code>
</td></tr></tbody>
</table></div><blockquote>
</blockquote>

<p>
All these types can be created by calling them with an optional
initializer of the correct type and value:
<div class="verbatim"><pre>
&gt;&gt;&gt; c_int()
c_long(0)
&gt;&gt;&gt; c_char_p("Hello, World")
c_char_p('Hello, World')
&gt;&gt;&gt; c_ushort(-3)
c_ushort(65533)
&gt;&gt;&gt;
</pre></div>

<p>
Since these types are mutable, their value can also be changed
afterwards:
<div class="verbatim"><pre>
&gt;&gt;&gt; i = c_int(42)
&gt;&gt;&gt; print i
c_long(42)
&gt;&gt;&gt; print i.value
42
&gt;&gt;&gt; i.value = -99
&gt;&gt;&gt; print i.value
-99
&gt;&gt;&gt;
</pre></div>

<p>
Assigning a new value to instances of the pointer types <tt class="class">c_char_p</tt>,
<tt class="class">c_wchar_p</tt>, and <tt class="class">c_void_p</tt> changes the <em>memory location</em> they
point to, <em>not the contents</em> of the memory block (of course not,
because Python strings are immutable):
<div class="verbatim"><pre>
&gt;&gt;&gt; s = "Hello, World"
&gt;&gt;&gt; c_s = c_char_p(s)
&gt;&gt;&gt; print c_s
c_char_p('Hello, World')
&gt;&gt;&gt; c_s.value = "Hi, there"
&gt;&gt;&gt; print c_s
c_char_p('Hi, there')
&gt;&gt;&gt; print s                 # first string is unchanged
Hello, World
&gt;&gt;&gt;
</pre></div>

<p>
You should be careful, however, not to pass them to functions
expecting pointers to mutable memory. If you need mutable memory
blocks, ctypes has a <code>create_string_buffer</code> function which creates
these in various ways.  The current memory block contents can be
accessed (or changed) with the <code>raw</code> property, if you want to access
it as NUL terminated string, use the <code>string</code> property:
<div class="verbatim"><pre>
&gt;&gt;&gt; from ctypes import *
&gt;&gt;&gt; p = create_string_buffer(3)      # create a 3 byte buffer, initialized to NUL bytes
&gt;&gt;&gt; print sizeof(p), repr(p.raw)
3 '\x00\x00\x00'
&gt;&gt;&gt; p = create_string_buffer("Hello")      # create a buffer containing a NUL terminated string
&gt;&gt;&gt; print sizeof(p), repr(p.raw)
6 'Hello\x00'
&gt;&gt;&gt; print repr(p.value)
'Hello'
&gt;&gt;&gt; p = create_string_buffer("Hello", 10)  # create a 10 byte buffer
&gt;&gt;&gt; print sizeof(p), repr(p.raw)
10 'Hello\x00\x00\x00\x00\x00'
&gt;&gt;&gt; p.value = "Hi"      
&gt;&gt;&gt; print sizeof(p), repr(p.raw)
10 'Hi\x00lo\x00\x00\x00\x00\x00'
&gt;&gt;&gt;
</pre></div>

<p>
The <code>create_string_buffer</code> function replaces the <code>c_buffer</code>
function (which is still available as an alias), as well as the
<code>c_string</code> function from earlier ctypes releases.  To create a
mutable memory block containing unicode characters of the C type
<code>wchar_t</code> use the <code>create_unicode_buffer</code> function.

<p>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="14.14.1.3 calling functions"
  href="ctypes-calling-functions.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="14.14.1 ctypes tutorial"
  href="ctypes-ctypes-tutorial.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="14.14.1.5 calling functions, continued"
  href="ctypes-calling-functions-continued.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="ctypes-calling-functions.html">14.14.1.3 Calling functions</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="ctypes-ctypes-tutorial.html">14.14.1 ctypes tutorial</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="ctypes-calling-functions-continued.html">14.14.1.5 Calling functions, continued</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
